<?php
$coupon = '';
$subtotalAfterDiscount = '';
$dicountPrice = '';
$AllDataCart = Yii::app()->shoppingCart->getPositions();
$Allclasscourseid = '';
$Allbatchid = '';
$Allbatchdate = '';
$Allbatchtiming = '';
$Allbatchseat = '';
$description_class_batch = '';
$couponCode = (Yii::app()->user->hasState('appliedcoupon')) ? Yii::app()->user->getState('appliedcoupon') : '';

$username = '';
$useremail = '';
$loginuserId = getLoginUserId();
if (!empty($loginuserId)) {
    $loginuserDetail = Users::getFrontEndUser($loginuserId);
    $username = !empty($loginuserDetail->frist_name) ? $loginuserDetail->frist_name : $loginuserDetail->user->username;
    $useremail = Yii::app()->user->name;
    ;
} else {
    $loginuserId = '';
}
?>
<style>
    .padding5px { padding: 5px; }
    .paddingleft5px { padding:5px 0px 0px 19px; }
    .cartBlock .col1 {width: 15%;}

    .cartBlock .mycol1 {width: 7%;}
    .span121 {width: 990px !important;}
    .checkoutcontainer{width: 912px !important;}


</style>
<div class="container checkoutcontainer">
    <div class="row">

        <div class="cartBlock">
            <!--Shopping Cart Heading Elements-->
            <div class="th">
                <div class="col mycol1">
                    <div class="padding5px">
                        <h5>Sr. No</h5>
                    </div>
                </div>
                <div class="col col2">
                    <div class="padding5px">
                        <h5>Workshop / Course</h5>
                    </div>
                </div>
                <div class="col col3 hidden-phone">
                    <div class="padding5px">
                        <h5>Qty</h5>
                    </div>
                </div>
                <div class="col col4 hidden-phone">
                    <div class="padding5px">
                        <h5>Total</h5>
                    </div>
                </div>

                <div class="col col4 hidden-phone">
                    <div class="padding5px">
                        <h5>Discount</h5>
                    </div>
                </div>


                <div class="clear"></div>
            </div>	
            <!--Shopping Cart Heading Elements Close-->
            <div style='overflow-y: auto;max-height:210px;'> 
                <!--Shopping Cart Product Info Start-->

                <?php
                $coupon = Coupons::getCouponType($couponCode);
                $subtotal = 0;
                $afterDicountBatchPrice = '';
                $cartCount = count($AllDataCart);
                if ($cartCount > 0) {
                    $i = 1;
                    foreach ($AllDataCart as $AllDataCart) {
                        if (!empty($coupon)) {
                            if ($coupon->apply_discount_to == 1) {
                                $priceByCalss = $AllDataCart->batchprice;
                                $subtotal = $subtotal + $priceByCalss;
                                if ($cartCount == $i) {
                                    $subtotalAfterDiscount = Coupons::getOrderofSubtotal($subtotal);
                                    $dicountPrice = Coupons::getDiscountPrice($coupon);
                                }
                            }
                            if ($coupon->apply_discount_to == 2) {
                                $priceByCalss = $AllDataCart->batchprice;
                                $subtotal = $subtotal + $priceByCalss;
                                if ($cartCount == $i) {
                                    $subtotalAfterDiscount = Coupons::totalofmatchingproducts($couponCode);
                                    $dicountPrice = Coupons::getDiscountPrice($coupon);
                                }
                            }
                            if ($coupon->apply_discount_to == 3) {
                                $classArray = Coupons::allmatchingproducts($couponCode);
                                if (in_array($AllDataCart->classes_id, $classArray)) {
                                    $afterDicountBatchPrice = ClassBatch::getDiscountOnClass($couponCode, $AllDataCart->price, $AllDataCart->quantity, $AllDataCart->id);
                                    $subtotalAfterDiscount = $afterDicountBatchPrice + $subtotalAfterDiscount;
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $dicountPrice = Coupons::getDiscountPrice($coupon);
                                    $subtotal = $subtotal + $priceByCalss;
                                } else {
                                    $afterDicountBatchPrice = '';
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $subtotalAfterDiscount = $priceByCalss + $subtotalAfterDiscount;
                                    $subtotal = $subtotal + $priceByCalss;
                                }
                            }
                            if ($coupon->apply_discount_to == 4) {
                                $chepeastBatchPrice = Coupons::cheapestmatchingproducts();
                                $afterDicountBatchPrice = ClassBatch::getChepeastPrice($chepeastBatchPrice, $AllDataCart->quantity, $couponCode, $AllDataCart->classes_id, $AllDataCart->price, $AllDataCart->id);
                                if (!empty($afterDicountBatchPrice)) {
                                    $dicountPrice = Coupons::getDiscountPrice($coupon);
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $subtotalAfterDiscount = $subtotalAfterDiscount + $afterDicountBatchPrice;
                                    $subtotal = $subtotal + $priceByCalss;
                                } else {
                                    $afterDicountBatchPrice = '';
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $subtotalAfterDiscount = $subtotalAfterDiscount + $priceByCalss;
                                    $subtotal = $subtotal + $priceByCalss;
                                }
                            }

                            if ($coupon->apply_discount_to == 5) {
                                $expensiveBatchPrice = Coupons::mostexpensivematchingproducts();
                                $afterDicountBatchPrice = ClassBatch::getChepeastPrice($expensiveBatchPrice, $AllDataCart->quantity, $couponCode, $AllDataCart->classes_id, $AllDataCart->price, $AllDataCart->id);
                                if (!empty($afterDicountBatchPrice)) {
                                    $dicountPrice = Coupons::getDiscountPrice($coupon);
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $subtotalAfterDiscount = $subtotalAfterDiscount + $afterDicountBatchPrice;
                                    $subtotal = $subtotal + $priceByCalss;
                                } else {
                                    $afterDicountBatchPrice = '';
                                    $priceByCalss = $AllDataCart->batchprice;
                                    $subtotalAfterDiscount = $subtotalAfterDiscount + $priceByCalss;
                                    $subtotal = $subtotal + $priceByCalss;
                                }
                            }
                        } else {
                            $priceByCalss = $AllDataCart->batchprice;
                            $subtotalAfterDiscount = $subtotalAfterDiscount + 50;
                            $subtotal = $subtotal + $priceByCalss;
                        }
                        if (isset($AllDataCart->classes_id)) {
                            $classes = Classes::model()->findByPk($AllDataCart->classes_id);
                            $classcoursetitle = $classes->class_titile;
                            $classcourseid = $classes->id;
                            $viewUrl = '/classes/viewClass/' . $classcourseid;
                            $classcoursetype = 'classes';
                        }
                        $imagePath = ($classes->users_id == '') ? 'backend/' . $classes->backend_user_id : $classes->users_id;
                        $imageName = (ClassImage::getPrimaryImage($classcourseid) != '') ? baseUrl() . '../../' . SkillConfig::getClassCourseImagePath($imagePath) . '240X180/' . ClassImage::getPrimaryImage($classcourseid) : baseUrl() . '../../' . getUploadDir() . '/no-class-image.jpg';
                        $imageName = '<img class="cartProduct" src="' . $imageName . '" alt="' . $classcoursetitle . '" />';
                        ?>

                        <div class="tr">
                            <div class="col mycol1">
                                <div class="padding5px">
                                    <p class="remmarginB inline-block"><?php echo $i; ?>.</p>
                                </div>
                            </div>                                                                 
                            <div class="col col2">
                                <div class="padding5px">
                                    <div class="productDetail">
                                        <div class="col-1"><?php echo CHtml::link($imageName, array($viewUrl)); ?></div>
                                        <div class="col-2"><p class="productName"><?php echo CHtml::link($classcoursetitle, array($viewUrl)); ?></p></div>
                                        <div class="col-3"><a href="javascript:void(0);" id="<?php echo $AllDataCart->id; ?>" class="btn-green"  onclick="return removeClassBatch(<?php echo $AllDataCart->id; ?>);">Remove</a></div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="col col3">
                                <div class="padding5px paddingleft5px">
                                    <p class="visible-phone txt000 font-opensansbd fnt-size14">Qty</p>
                                    <input type="text" class="field" value="<?php echo $AllDataCart->quantity; ?>" onchange="return QuantityUpdate(this.value,<?php echo $AllDataCart->id; ?>,'<?php echo $classcoursetype; ?>','errorSeats<?php echo $AllDataCart->id; ?>','<?php echo $AllDataCart->batchtiming; ?>','<?php echo $AllDataCart->startdate; ?>','<?php echo $AllDataCart->class_venue_id; ?>','<?php echo $AllDataCart->classes_id; ?>');"><p id="errorSeats<?php echo $AllDataCart->id; ?>" class="errorMessage"></p>
                                </div>
                            </div>
                            <div class="col col4">
                                <div class="padding5px paddingleft5px">
                                    <p class="visible-phone txt000 remmarginB font-opensansbd fnt-size14">Total</p><p class="remmarginB inline-block"> <?php echo (!empty($afterDicountBatchPrice)) ? '<strike> Rs. ' . number_format($priceByCalss, 2) . '</strike><br> Rs. ' . number_format($afterDicountBatchPrice, 2) : 'Rs. ' . number_format($priceByCalss, 2); ?></p>
                                </div>
                            </div>

                            <div class="col col4">
                                <div class="padding5px paddingleft5px">
                                    <p class="remmarginB inline-block">
                                        <?php
                                        $booking_price = ClassBatch::getBatchPriceBySeatsInt($AllDataCart->id, $AllDataCart->quantity, 'txt');
                                        if ($booking_price)
                                            $booking_price; else
                                            echo "No Discount";
                                        ?>
                                    </p>
                                </div>
                            </div>								
                            <div class="clear"></div>
                        </div>	

                        <?php
                        $i++;
                    }
                    ?>

                </div>

                <div class="cartBottom" style="border-top: 1px solid;">
                    <!--Discount Area-->
                    <div class="left">
                        <div class="padding5px">
                            <input type="text" class="discountField" id="discount_id" name="discount_id" placeholder="Enter Discount Code"><input type="button" class="applybtn" value="Apply" onclick="return AddDiscount();">
                            <span class="errorMessage" id="discount_error" style="display:none;"></span>
                        </div>
                    </div>
                    <div class="right">
                        <div class="padding5px">
                            <?php if (!empty($dicountPrice)) { ?>
                                <h6 class="fnt-size12 txt000" id="priceTotal">Grand Total :   <strike>Rs. <?php echo number_format($subtotal, 2); ?></strike></h6>
                                <h6 class="fnt-size12 txt000" id="priceTotal">Discount  : <?php echo $dicountPrice; ?></h6>
                                <h6 class="fnt-size12 txt000" id="priceTotal">Sub Total  : <?php echo $subtotalAfterDiscount; ?></h6>
                            <?php } else { ?>
                                <h6 class="fnt-size12 txt000" id="priceTotal">Sub Total :   Rs. <?php echo number_format($subtotal, 2); ?></h6>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                <div class="alignR">

                    <?php
                    Yii::app()->session['totalamountcart'] = $subtotal;
                    $this->renderPartial('application.www.themes.front-end.views..shoppingCart._form_EBS', array('subtotal' => $subtotal, 'loginuserId' => $loginuserId, 'useremail' => $useremail, 'username' => $username));
                    ?>
                </div>
                <!--Shopping Cart End-->

            <?php } else {
                Coupons::unsetSessionShopingcart();
                if(getLoginUserId()){
					ShoppingCartByUser::model()->deleteAll('user_id=' . getLoginUserId());
				}
                ?>
                <div class="padding30px" align="center">
                    <p>There are no items in this cart.</p>
                <?php echo CHtml::link('Continue Shopping', SITE_URL, array('class' => 'btn')); ?>
                </div>
<?php } ?>
        </div>

    </div>
</div>

<script type="text/javascript">   
    /* Remove from shopping cart */
    function removeClassBatch(id){
        var resultconfirmdel = confirm('Are you sure want to remove from cart ?');
        if (resultconfirmdel != false) {
            $.post("<?php echo Yii::app()->createUrl("ShoppingCart/delete"); ?>", 
            {batchId: id}, function(result) {
                if(result=="success"){
                    openShopCart();
                }
            });
        }
        //});
    }
    /* Update shopping cart by quantity */
    function QuantityUpdate(qty,id,classcoursetype,displayid,batchtiming,startdate,venueid,classes_id){
        // jConfirm('Are you sure want to update quantity ?', function (resultconfirm) {
        var resultconfirm = confirm('Are you sure want to update quantity ?');
        if (resultconfirm != false) {
            $.post("<?php echo Yii::app()->createUrl("ShoppingCart/update"); ?>", 
            {batchId: id,qty: qty,classcoursetype: classcoursetype,startdate: startdate,batchtiming: batchtiming,classes_id: classes_id}, function(result) {
                if(result=="success"){
                    openShopCart();
                }else{
                    var noofseats = result.split("#@#");
                    var totalSeats = noofseats[0];
                    var expactedSeats = noofseats[1];
                    $("#"+displayid).html("Availability:"+totalSeats);
                    setTimeout(function() {   //calls click event after a certain time
                        openShopCart();
                    }, 1000);
					
					
					
                }
            });
        }
        // });
    }

    /* Update shopping cart by quantity */
    function AddDiscount(){
        var matchIds="";
        var discount_code = $("#discount_id").val();
        var total_amt = $("#amount").val();
        if(discount_code==""){
            $("#discount_error").html("Please insert value");
        }else{
            $.post("<?php echo Yii::app()->createUrl("ShoppingCart/addDiscount"); ?>", 
            {discount_code: discount_code,total_amt:total_amt}, function(result) {               
                if(result!=1){                    
                    $("#discount_error").show();
                    $("#discount_error").html("This coupon not valid!");
                }else{
                    openShopCart();
                }						
            });
	
        }
    }

</script>